package com.wt.dao;

import com.wt.entity.WqunAccount;
import com.wt.entity.WqunCommunity;
import com.wt.util.R.R;
import com.wt.util.jdbc.Util;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class WqunCommunityDao {
    //建立数据库连接，以便进行事务处理
    Connection con;
    {
        try {
            con = Util.getCon().getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 查询数据库，方法由 service 层 调用
     */
    //实现化数据访问层中QueryRunner对象
    private QueryRunner qr = new QueryRunner(Util.getCon());


    public int countByUid(String id) {
        try {
            String sql = "SELECT count(*) FROM wqun_community where `uid` = ?";
            Long count = (Long) qr.query(sql, new ScalarHandler(),id);
            return Math.toIntExact(count);
        } catch (SQLException throwables) {
            return 0;
        }
    }

    public boolean updateById(WqunCommunity wqunCommunity){
        try {
            String sql =
                    "UPDATE wqun_community  SET `type` = ? where id = ?";

            return  qr.update(sql,wqunCommunity.getType(),wqunCommunity.getId()) > 0;

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }



    /**
     * 查询前50条信息
     */
    public List<WqunCommunity> findAllComment(){
        try {
            String sql = "SELECT id,crowd_no crowdNo,uid,avatar,to_uid toUid,to_user_uid  toUserUid,content,file_name fileName,is_delete isDelete,`type`,created_time createdTime,modified_time modifiedTime FROM wqun_community ORDER BY id desc limit 0,50";
            return qr.query(sql, new BeanListHandler<WqunCommunity>(WqunCommunity.class));
        } catch (SQLException throwables) {
            return null;
        }
    }

    public List<WqunCommunity> getById(int id){
        try {
            String sql = "SELECT id,crowd_no crowdNo,uid,avatar,to_uid toUid,to_user_uid  toUserUid,content,file_name fileName,is_delete isDelete,`type`,created_time createdTime,modified_time modifiedTime FROM wqun_community where id=?";
            return qr.query(sql, new BeanListHandler<WqunCommunity>(WqunCommunity.class),id);
        } catch (SQLException throwables) {
            return null;
        }
    }

    /**
     * 查询最后一条信息
     */
    public List<WqunCommunity> getLastOne(){
        try {
            String sql = "SELECT id,crowd_no crowdNo,uid,avatar,to_uid toUid,to_user_uid  toUserUid,content,file_name fileName,is_delete isDelete,`type`,created_time createdTime,modified_time modifiedTime FROM wqun_community ORDER BY id desc limit 0,1";
            return qr.query(sql, new BeanListHandler<WqunCommunity>(WqunCommunity.class));
        } catch (SQLException throwables) {
            return null;
        }
    }

    public boolean save(WqunCommunity wqunCommunity){
        try {
            String sql1 =
                    "insert into wqun_community values(null,null,?,?,null,null,?,?,0,?,?,?) ";
            //调用 qr 对象中的方法 update 执行添加,自动添加
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String createdTime = simpleDateFormat.format(date);
            return qr.update(sql1, wqunCommunity.getUid(),wqunCommunity.getAvatar(),wqunCommunity.getContent(),wqunCommunity.getFileName(),wqunCommunity.getType(),createdTime,createdTime)>0;
            //System.out.println(iret > 0 ? "添加成功":"添加失败");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }
}
